package com.open.capacity.workflow3.service.impl;

import cn.hutool.db.DaoTemplate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow3.dao.NwfDelegateRelationDao;
import com.open.capacity.workflow3.dao.NwfDelegateRuleDao;
import com.open.capacity.workflow3.entity.NwfDelegateRelation;
import com.open.capacity.workflow3.entity.NwfDelegateRule;
import com.open.capacity.workflow3.entity.NwfProcess;
import com.open.capacity.workflow3.service.NwfDelegateService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class NwfDelegateServiceImpl implements NwfDelegateService {

    @Autowired
    private NwfDelegateRuleDao ruleDao;

    @Autowired
    private NwfDelegateRelationDao relationDao;


    @Override
    public PageResult<NwfDelegateRule> getPageList(Map<String, Object> params, String keyword, SysUser userInfo) {
        if (!userInfo.getId().equals("1")) {
            params.put("createUserid", userInfo.getId());
        }

        if (StringUtils.isNotEmpty(keyword)) {
            params.put("keyword", keyword);
        }
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null) PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<NwfDelegateRule> list = ruleDao.findAll(params);
        PageInfo<NwfDelegateRule> pageInfo = new PageInfo(list);

        return PageResult.<NwfDelegateRule>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }

    @Override
    public List<NwfDelegateRule> getList(SysUser userInfo) {
        Map<String, Object> params = new HashMap<>();
        params.put("toUserid", userInfo.getId());
        params.put("currentDate", new Date());

        List<NwfDelegateRule> list = ruleDao.findAll(params);
        return list;
    }

    @Override
    public List<NwfDelegateRelation> getRelationList(String keyValue) {
        return relationDao.findByDelegateRuleId(keyValue);
    }

    @Override
    @Transactional
    public void delete(String keyValue) {
        ruleDao.delete(keyValue);
        relationDao.findByDelegateRuleId(keyValue);
    }

    @Override
    public void save(String keyValue, NwfDelegateRule wfDelegateRuleEntity, String[] schemeInfoList) {
        SysUser user = SysUserUtil.getLoginAppUser();
        if (StringUtils.isEmpty(keyValue)) {
            wfDelegateRuleEntity = wfDelegateRuleEntity.create();

            wfDelegateRuleEntity.setCreateUserid(user.getId());
            wfDelegateRuleEntity.setCreateUsername(user.getRealName());

            ruleDao.save(wfDelegateRuleEntity);
        } else {
            wfDelegateRuleEntity = wfDelegateRuleEntity.modify(keyValue);
            ruleDao.update(wfDelegateRuleEntity);
            relationDao.deleteByDelegateRuleId(keyValue);
        }
        for (String schemeInfoId : schemeInfoList) {
            NwfDelegateRelation wfDelegateRuleRelationEntity = new NwfDelegateRelation();
            wfDelegateRuleRelationEntity.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            wfDelegateRuleRelationEntity.setDelegateRuleId(wfDelegateRuleEntity.getId());
            wfDelegateRuleRelationEntity.setSchemeInfoId(schemeInfoId);
            relationDao.save(wfDelegateRuleRelationEntity);
        }
    }

    @Override
    public void updateState(String keyValue, int state) {
        NwfDelegateRule wfDelegateRuleEntity = new NwfDelegateRule();
        wfDelegateRuleEntity.setId(keyValue);
        wfDelegateRuleEntity.setEnabledMark(state);

        ruleDao.update(wfDelegateRuleEntity);
    }
}
